#!/bin/sh
G_sh=1  #数值为1，启动守护脚本。0就不启动。
G_bb="170209_mipsel_"  #程序版本
##
G_RZ="/tmp/log/koolproxy.log"   #日志位置
G_WZ="/bin/01kool.sh"
G_ML="/tmp/G_RAM/koolproxy/"  #koolproxy安装目录
G_OSC="https://git.oschina.net/g0g001/tmp_G_RAM/raw/master"
G_HUB="https://raw.githubusercontent.com/idjdb/tmp_G_RAM/master"
G_WJT=https://rules.ngrok.wang/koolproxy.txt
G_WJD=https://rules.ngrok.wang/1.dat
G_WJV=https://rules.ngrok.wang/config.json.js
G_IP=180.76.76.76
G_BL=0
G_JGG=5
while [ "1" ] ;
do
sleep $G_JGG
let G_BL=$G_BL+1
if [ "$G_BL" -le 20 ]
then
G_JGG=30
else
G_JGG=600
fi
if ping -w 1 -c 1 "$G_IP"
then
G_CGZ=0
wget -qO /tmp/G_RAM.tar.gz "$G_OSC"/"$G_bb"G_RAM.tar.gz || wget -qO /tmp/G_RAM.tar.gz "$G_HUB"/"$G_bb"G_RAM.tar.gz
tar -zxpf /tmp/G_RAM.tar.gz -C /tmp/ && G_CGZ=1 || G_CGZ=0
rm /tmp/G_RAM.tar.gz
if [ "$G_CGZ" -eq 1 ]
then
wget -qO "$G_ML"koolproxy.txt "$G_WJT" || wget -qO "$G_ML"koolproxy.txt "$G_OSC"/koolproxy.txt || echo $(date +"%Y-%m-%d %H:%M:%S")  "【下载失败】——koolproxy.txt静态规则！" >> "$G_RZ"
wget -qO "$G_ML"1.dat "$G_WJD" || wget -qO "$G_ML"1.dat "$G_OSC"/1.dat || echo $(date +"%Y-%m-%d %H:%M:%S")  "【下载失败】——1.dat视频规则！" >> "$G_RZ"
wget -qO "$G_ML"version "$G_WJV" || wget -qO "$G_ML"version "$G_OSC"/version || echo $(date +"%Y-%m-%d %H:%M:%S")  "【下载失败】——检验文件！" >> "$G_RZ"
chmod +x "$G_ML"*
"$G_ML"init.d/koolproxy restart
G_ZF=`ps | grep "koolproxy -d" | wc -l`
if [ "$G_ZF" -eq 3 ];then
echo -e "\n"$(date +"%Y-%m-%d %H:%M:%S")         "【"$G_BL"】次--KoolProxy【安装成功】！" >> "$G_RZ"
/usr/share/koolproxy/koolproxyupdate&
if [ "$G_sh" -eq 1 ];then
sh "$G_WZ"&
echo $(date +"%Y-%m-%d %H:%M:%S")        "守护脚本【启动成功】" >> "$G_RZ"
fi
exit 0
else
"$G_ML"init.d/koolproxy stop&
if [ "$G_BL" -gt 50 ]
then
echo $(date +"%Y-%m-%d %H:%M:%S")        "【放弃安装】【致命错误】【"$G_BL"】次安装koolproxy失败！" >> "$G_RZ"
exit 0
else
echo $(date +"%Y-%m-%d %H:%M:%S")        "【安装失败】无法启动程序。"$G_JGG"秒后再次安装！" >> "$G_RZ"
fi
fi
else
echo $(date +"%Y-%m-%d %H:%M:%S")        "【安装失败】无法下载基础安装包。"$G_JGG"秒后再次安装！" >> "$G_RZ"
fi


else
if [ "$G_BL" -gt 50 ]
then
echo $(date +"%Y-%m-%d %H:%M:%S")        "【放弃安装】【不具备上网环境】【"$G_BL"】次安装koolproxy失败！" >> "$G_RZ"
exit 0
else
echo $(date +"%Y-%m-%d %H:%M:%S")        "【无法联网】"$G_JGG"秒后再次安装！" >> "$G_RZ"
fi
fi
done